package cn.fl.product.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.ISplStepRebatesService;
import cn.fl.product.transform.SplStepRebatesVOTransform;
import cn.fl.product.vo.SplStepRebatesVO;
import cn.fl.product.vo.StepRebatesDetailVO;
import fl.spl.facade.splrebates.api.ISplStepRebatesApiService;
import fl.spl.facade.splrebates.condition.SplStepRebatesInfoQC;
import fl.spl.facade.splrebates.dto.SplStepRebatesInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SplStepRebatesServiceImpl implements ISplStepRebatesService {
    @Autowired
    private ISplStepRebatesApiService splStepRebatesApiService ;
    @Override
    public List<SplStepRebatesVO> searchList(SplStepRebatesInfoQC condition) throws BaseException {
        List<SplStepRebatesInfoDTO> dtoList = splStepRebatesApiService.searchList(condition, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
        return SplStepRebatesVOTransform.toVOList(dtoList);
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        return splStepRebatesApiService.searchListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public Long add(SplStepRebatesVO vo) throws BaseException {

        return splStepRebatesApiService.add(SplStepRebatesVOTransform.toDTO(vo));
    }

    @Override
    public boolean modufy(SplStepRebatesVO vo) throws BaseException {

        return splStepRebatesApiService.modify(SplStepRebatesVOTransform.toDTO(vo));
    }

    @Override
    public SplStepRebatesVO getVOById(Long id) throws BaseException {
        SplStepRebatesInfoDTO dto = this.splStepRebatesApiService.selectById(id);
        SplStepRebatesVO vo = SplStepRebatesVOTransform.toVO(dto);
        List<StepRebatesDetailVO>voList = vo.getDetailVOList();
        if(CollectionUtils.isNotEmpty(voList)){
            for (StepRebatesDetailVO detailVO: voList) {
                if(StringUtils.isNotEmpty(detailVO.getStep())){
                    switch (detailVO.getStep()){
                        case 1 :
                            vo.setStarAmt_1(DecimalUtils.div((double)detailVO.getStarAmt(),1000000.0));
                            vo.setEndAmt_1(DecimalUtils.div((double)detailVO.getEndAmt(),1000000.0));
                            vo.setRate_1(detailVO.getRate());
                            break;
                        case 2:
                            vo.setStarAmt_2(DecimalUtils.div((double)detailVO.getStarAmt(),1000000.0));
                            vo.setEndAmt_2(DecimalUtils.div((double)detailVO.getEndAmt(),1000000.0));
                            vo.setRate_2(detailVO.getRate());
                            break;
                        case 3:
                            vo.setStarAmt_3(DecimalUtils.div((double)detailVO.getStarAmt(),1000000.));
                            vo.setEndAmt_3(DecimalUtils.div((double)detailVO.getEndAmt(),1000000.0));
                            vo.setRate_3(detailVO.getRate());
                            break;
                        case 4:
                            vo.setStarAmt_4(DecimalUtils.div((double)detailVO.getStarAmt(),1000000.0));
                            vo.setEndAmt_4(DecimalUtils.div((double)detailVO.getEndAmt(),1000000.0));
                            vo.setRate_4(detailVO.getRate());
                            break;
                        default:
                            break;
                    }

                }
            }
        }
        return vo;
    }
}
